import re


def extract_atoms_info() -> str:
    with open("atoms.md") as file:
        contents = file.read()
    paragraphs = contents.split("\n\n")
    entries = []
    for paragraph in paragraphs:
        arity = "unknown"
        for line in paragraph.splitlines():
            if line.startswith("##"):
                if match := re.search(r"nilad|monad|dyad", line):
                    arity = match[0]
            elif match := re.fullmatch(r"`(.*?)`\|(.*)", line):
                name = match[1].replace("\\", "")
                description = match[2].replace("\\", "\\\\").replace("'", "\\'")
                entries.append(
                    f"  ['{name}'] = "
                    "{\n"
                    f"    kind = '{arity}',\n"
                    f"    description = '{description}',\n"
                    "  },\n"
                )
    return "".join(entries)


lua = "return {\n" + extract_atoms_info() + "}"
with open("../lua/info.lua", "w") as file:
    file.write(lua)
